package com.zhanglijie.base.sync.mardwordDetail;

import org.openjdk.jol.info.ClassLayout;

/**
 * @author zhanglijie
 * @version 1.0
 * @since 1.1.0 2022/6/18 0018 14:32
 */
public class NoLockToSlowToNoLockTest {
    static MyObject myObject = null;

    public static void main(String[] args) {
        /**
         * 无锁-》轻量级锁-》无锁
         */
        myObject = new MyObject();
        //偏向锁验证需要睡眠5秒再打印
        try{Thread.sleep(5000);}catch (Exception e){}
        System.out.println("加锁之前");
        System.out.println(ClassLayout.parseInstance(myObject).toPrintable());
        //加锁
        new NoLockToSlowToNoLockTest().testLock();
        System.out.println("加锁之后");
        System.out.println(ClassLayout.parseInstance(myObject).toPrintable());
    }
    public void testLock(){
        synchronized (myObject){
            System.out.println("已经加锁");
            System.out.println(ClassLayout.parseInstance(myObject).toPrintable());
            System.out.println("将退出锁");
        }
    }
}
